package com.easygoods.service;

import com.bestvike.stone.core.util.area.AreaUtils;
import com.bestvike.stone.core.util.area.City;
import com.bestvike.stone.core.util.area.County;
import com.bestvike.stone.core.util.area.Province;
import com.bestvike.stone.core.util.area.ProvinceCollection;
import com.bestvike.stone.core.util.area.Town;
import com.bestvike.stone.spring.service.BaseService;
import com.easygoods.dao.DistrictInfoDao;
import com.easygoods.entity.DistrictInfo;
import com.easygoods.enums.YNEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 许崇雷 on 2019-01-09.
 */
@Service
public class DistrictService extends BaseService {
    @Autowired
    private DistrictInfoDao districtInfoDao;

    //初始化 省市区 表
    public void init() {
        int count = this.districtInfoDao.selectCount(null);
        if (count > 0) {
            this.log.warn("DistrictInfo has already inited.");
            return;
        }

        ProvinceCollection provinceCollection = AreaUtils.getProvinceCollection();
        for (Province province : provinceCollection) {
            DistrictInfo provinceBean = new DistrictInfo();
            provinceBean.setParentId("0");
            provinceBean.setId(province.getCode());
            provinceBean.setDistName(province.getName());
            provinceBean.setHasChildren(YNEnum.parse(province.getCityCollection().size() > 0).name());
            provinceBean.beforeInsert();
            this.districtInfoDao.insert(provinceBean);
            for (City city : province.getCityCollection()) {
                DistrictInfo cityBean = new DistrictInfo();
                cityBean.setParentId(province.getCode());
                cityBean.setId(city.getCode());
                cityBean.setDistName(city.getName());
                cityBean.setHasChildren(YNEnum.parse(city.getCountyCollection().size() > 0).name());
                cityBean.beforeInsert();
                this.districtInfoDao.insert(cityBean);
                for (County county : city.getCountyCollection()) {
                    DistrictInfo countyBean = new DistrictInfo();
                    countyBean.setParentId(city.getCode());
                    countyBean.setId(county.getCode());
                    countyBean.setDistName(county.getName());
                    countyBean.setHasChildren(YNEnum.parse(county.getTownCollection().size() > 0).name());
                    countyBean.beforeInsert();
                    this.districtInfoDao.insert(countyBean);
                    for (Town town : county.getTownCollection()) {
                        DistrictInfo townBean = new DistrictInfo();
                        townBean.setParentId(county.getCode());
                        townBean.setId(town.getCode());
                        townBean.setDistName(town.getName());
                        townBean.setHasChildren(YNEnum.N.name());
                        townBean.beforeInsert();
                        this.districtInfoDao.insert(townBean);
                    }
                }
            }
        }
    }
}
